
#include<stdio.h>
#include<string.h>


struct ListNode {
    int val;
    struct ListNode* next;
};

struct ListNode* detectCycle(struct ListNode* head) {
    struct ListNode* fast = head;
    struct ListNode* slow = head;
    struct ListNode* cur = head;
    struct ListNode* meet = head;

    while (fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast) {
            meet = slow;
            while (meet != cur)
            {
                meet = meet->next;
                cur = cur->next;
            }
            return cur;
        }
    }
    return NULL;
}
